package com.example.dbsample.model.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dbsample.util.spring.PageUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("分页返回参数")
public class PageVo<T> {

    @ApiModelProperty("分页内容")
    private List<T> list;

    @ApiModelProperty(value = "总页数")
    private Integer totalPage;

    @ApiModelProperty(value = "总条数")
    private Long total;

    @ApiModelProperty(value = "当前页")
    private Integer currentPage;

    @ApiModelProperty(value = "每页数量")
    private Integer pageSize;

    // 两个T
    public static <T> PageVo<T> create(long total, List<T> list) {
        PageVo<T> pageVo = new PageVo<>();
        pageVo.setTotal(total);
        pageVo.setList(list);
        return pageVo;
    }

    public static <T> List<T> getPageList(List<T> list, int size, int page) {
        if(CollectionUtils.isEmpty(list)) {
            return list;
        }

        int totalSize = list.size();
        // 总数只有一页
        if(totalSize <= size) {
            return list;
        }

        int startIndex = Math.max(size*(page-1), 0);
        int endIndex = Math.min(size*page, totalSize);
        if(startIndex > totalSize) {
            return new ArrayList<>();
        } else {
            return list.subList(startIndex, endIndex);
        }
    }


    // TODO 引入jar会导致无法启动，暂时
    //public static <T> PageVo<T> toPageVo(PageInfo<T> page) {
    //    return PageVo.<T>builder()
    //            .list(page.getList())
    //            .totalPage(page.getPages())
    //            .total(page.getTotal())
    //            .currentPage(page.getPageNum())
    //            .pageSize(page.getPageSize())
    //            .build();
    //}
    //
    //@ApiModelProperty(value = "是时候mapper查出来的entity字段多，转换成其中某个字段的records")
    //public static <T> PageVo<T> buildByRecords(List<T> records, PageInfo<?> page) {
    //    return PageVo.<T>builder()
    //            .list(records)
    //            .totalPage(page.getPages())
    //            .total(page.getTotal())
    //            .currentPage(page.getPageNum())
    //            .pageSize(page.getPageSize())
    //            .build();
    //
    //}


    public static <T> PageVo<T> buildByPage(Page<T> page) {
        return PageVo.<T>builder()
                .list(page.getRecords())
                .totalPage(Math.toIntExact(page.getPages()))
                .total(page.getTotal())
                .currentPage(Math.toIntExact(page.getCurrent()))
                .pageSize(Math.toIntExact(page.getSize()))
                .build();

    }

    public static <T> PageVo<T> buildByRecords(List<T> records, Page<T> page) {
        return PageVo.<T>builder()
                .list(records)
                .totalPage(Math.toIntExact(page.getPages()))
                .total(page.getTotal())
                .currentPage(Math.toIntExact(page.getCurrent()))
                .pageSize(Math.toIntExact(page.getSize()))
                .build();

    }

    public static <T> PageVo<T> buildByParams(List<T> records, Long total, Integer currentPage, Integer pageSize) {
        int totalPage = (int) PageUtils.getPages(total, pageSize);
        return PageVo.<T>builder()
                .list(records)
                .totalPage(totalPage)
                .total(total)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();

    }

    public static <T> PageVo<T> buildEsByParams(List<T> records, Long total, Integer currentPage, Integer pageSize) {
        Long totalFront = total > 10000 ? 10000 : total;
        int totalPage = (int) PageUtils.getPages(totalFront, pageSize);
        return PageVo.<T>builder()
                .list(records)
                .totalPage(totalPage)
                .total(total)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();

    }
}
